<?php

namespace Modules\Chat\Http\Controllers;

use App\Helper\LogHelper;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redis;
use Modules\Chat\Base\BaseController;
use Modules\Chat\Models\ChatMessage;
use Modules\Chat\Models\ChatRoom;
use Modules\Chat\Models\ChatRoomJoin;
use Modules\Chat\Models\ChatUser;
use App\Helper\ErrorHelper;

class ChatController extends BaseController
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $from, $to)
    {
        LogHelper::info('start', 'start');
        // check parameter from and to
        // $members = Redis::zrange("laravel8_room:1" , 0 , -1);
        // dd($members);
        // 先以固定参数模拟
        // doctor 3464 68fd477c3e9a99fc177433fa012f8ed3
        // patient 472 978632fceeb08fdaad32538be0cfb9fd

//        $a = $this->service('Message')->sendMessage();
//        dd($a);

        // 创建聊天室

        // 创建2人的聊天室room
        $fromUser = ChatUser::where('remember_token', $from)->first();
        if (empty($fromUser))
        {
            $fromUser = new ChatUser();
            $fromUser->name = '刘医生';
            $fromUser->remember_token = $from;
            $fromUser->save();
        }

        $toUser = ChatUser::where('remember_token', $to)->first();
        if (empty($toUser))
        {
            $toUser = new ChatUser();
            $toUser->name = '刘患者';
            $toUser->remember_token = $to;
            $toUser->save();
        }

        $room = ChatRoom::find(1);
        if (empty($room))
        {
            $room = new ChatRoom();
            $room->title = '聊天室1';
            $room->user_id = $fromUser->id;
            $room->save();
        }

        $roomJoin = ChatRoomJoin::where('room_id', $room->id)->where('user_id', $toUser->id)->first();
        if(empty($roomJoin))
        {
            $roomJoin = new ChatRoomJoin();
            $roomJoin->room_id = $room->id;
            $roomJoin->user_id = $toUser->id;
            $roomJoin->save();
        }

        return view('chat::chat.chat', [
            'room'  => $room,
            'user'  => $fromUser,
            'fuser' => $toUser,
            'room'  => $room,
        ]);
    }

    public function getMessageHistory($room_id)
    {
        $data = ChatMessage::where('room_id', $room_id)->get();

        return response()->json(ErrorHelper::error('2000', $data), 200);
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('chat::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('chat::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('chat::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }
}
